package com.swing.sky.web.generator;

import com.swing.sky.web.generator.config.MybatisConfig;
import com.swing.sky.web.generator.dao.GenDAO;
import com.swing.sky.web.generator.domain.Column;
import com.swing.sky.web.generator.domain.Module;
import com.swing.sky.web.generator.domain.ModuleHouse;
import com.swing.sky.web.generator.util.ModuleHouseInitializer;
import com.swing.sky.web.generator.util.VelocityContextBuilder;
import com.swing.sky.web.generator.util.VelocityInitializer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;

/**
 * @author swing
 */
public class Gen {
    private static final Logger log = LoggerFactory.getLogger(Gen.class);
    GenDAO genDAO;

    public static void main(String[] args) {
        List<ModuleHouse> moduleHouses = ModuleHouseInitializer.buildModuleHouses();
        Gen genService = new Gen();
        for (ModuleHouse moduleHouse : moduleHouses) {
            for (Module module : moduleHouse.getModules()) {
                genService.generatorCode(moduleHouses, moduleHouse, module);
            }
        }
    }

    public void init() {
        try {
            genDAO = MybatisConfig.getDAO();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void generatorCode(List<ModuleHouse> moduleHouses, ModuleHouse moduleHouse, Module module) {
        init();
        //获取该表的的所有列对象
        Column column = new Column();
        column.setTableSchema(moduleHouse.getTable().getTableSchema());
        column.setTableName(moduleHouse.getTable().getTableName());
        List<Column> columns = genDAO.listColumns(column);

        //初始化模板引擎
        VelocityInitializer.initVelocity();

        Template template = Velocity.getTemplate(module.getTemplateUrl(), "UTF-8");

        //获取渲染模板需要的上下文
        VelocityContext velocityContext = VelocityContextBuilder.buildVelocityContext(moduleHouses, moduleHouse, module);
        //出入渲染的文件
        StringWriter stringWriter = new StringWriter();
        template.merge(velocityContext, stringWriter);

        //获取输出的文件文件夹路径
        File file = new File(module.getAbsolutePath());
        if (!file.exists()) {
            if (file.mkdirs()) {
                System.out.println("文件夹夹创建成功");
            }
        }
        //获取文件名
        String fileName = module.getFileName() + module.getExtension();
        //获取文件名并输出
        File file1 = new File(module.getAbsolutePath() + "\\" + fileName);

        if (!file1.exists()) {
            try {
                boolean newFile = file1.createNewFile();
                if (newFile) {
                    System.out.println("文件拆创建成功");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1), "UTF-8"));
            String s = stringWriter.toString();
            writer.write(s);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
